#include "historywidget.h"
#include "ui_historywidget.h"
#include "utils.h"

#include <QFile>

HistoryWidget::HistoryWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HistoryWidget)
{
    ui->setupUi(this);
    setWindowTitle(tr("历史记录"));
    // 单击右侧切换选择，双击左侧进入新的页面
    connect(this->ui->listDate, &QListWidget::itemClicked, this, &HistoryWidget::handleItemClick);

    connect(this->ui->listContent, &QListWidget::itemDoubleClicked, this, &HistoryWidget::handleItemDoubleClick);
}

HistoryWidget::~HistoryWidget()
{
    delete ui;
}


// 每次显示是重新加载历史记录数据
void HistoryWidget::showEvent(QShowEvent *event)
{
    m_historyRecord.clear();
    // 未加载过，重新加载
    if(m_historyRecord.count() == 0)
        m_historyRecord = Utils::loadHistoryRecord();
    ui->listContent->clear();
    for(int i = 0; i < m_historyRecord.size(); i++){
        qDebug()<<m_historyRecord[i].historyString();
//        QStringList localSplit = history[i].split("#");
//        ui->listContent->addItem(localSplit[0] + " , " + localSplit[1]);
         ui->listContent->addItem(m_historyRecord[i].historyString());
    }
}

bool HistoryWidget::caseInsensitiveLessThan(const HistoryRecord &s1, const HistoryRecord &s2)
  {
      return  s1.m_datetime > s2.m_datetime;
  }


void HistoryWidget::updateHistoryData(QDateTime startTime)
{
//    qDebug()<<"start time = "<<startTime.toString();
    m_historyRecord.clear();
    QVector<HistoryRecord> history = Utils::loadHistoryRecord();
    for(int i = 0; i < history.count(); i++){
        if(history[i].dateTime() > startTime){
            m_historyRecord.push_back(history[i]);
        }
    }
    // 排序
    qSort(m_historyRecord.begin(), m_historyRecord.end(), HistoryWidget::caseInsensitiveLessThan);
    ui->listContent->clear();
    for(int i = 0; i < m_historyRecord.count(); i++){
        ui->listContent->addItem(m_historyRecord[i].historyString());
    }
    qDebug()<<"count = "<<m_historyRecord.count();
}

void HistoryWidget::reloadHistoryData()
{
    m_historyRecord = Utils::loadHistoryRecord();
}



void HistoryWidget::handleItemClick(QListWidgetItem *item)
{
    int idx = ui->listDate->row(item);
    if(idx == 0){
        // 显示今天的历史记录
//        QDateTime time = QDateTime::currentDateTime().addSecs(-60 * 60 * 1);   // 过去一小时
        QDateTime time = QDateTime::currentDateTime().addDays(-1);
        updateHistoryData(time);
    }else if(idx == 1){
        // 显示过去7天的历史记录
        QDateTime time = QDateTime::currentDateTime().addDays(-7);
        updateHistoryData(time);
    }else if(idx == 2){
        // 显示过去一个月的历史记录
        QDateTime time = QDateTime::currentDateTime().addMonths(-1);
        updateHistoryData(time);
    }else{
        // 显示所有的历史记录
        updateHistoryData(QDateTime::currentDateTime().addYears(-10));
    }
}

void HistoryWidget::handleItemDoubleClick(QListWidgetItem *item)
{
    QString url = item->text().split("#")[1];
    emit loadUrl(url);
}
